<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: AdminSettingsController.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_AdminSettingsController extends Core_Controller_Action_Admin {
	
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'vote_admin_main', array (), 'vote_admin_main_settings' );
		
		$this->view->form = $form = new Vote_Form_Admin_Global ();
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = $form->getValues ();
			foreach ( $values as $key => $value ) {
				Engine_Api::_ ()->getApi ( 'settings', 'core' )->setSetting ( $key, $value );
			}
		}
	}
	
	public function categoriesAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'vote_admin_main', array (), 'vote_admin_main_categories' );
		
		$this->view->type = $type = $this->_getParam ( 'type', 'photo' );
		
		$this->view->categories = Engine_Api::_ ()->vote ()->getCategories ( $type );
	}
	
	public function addCategoryAction() {
		// Generate and assign form
		$form = $this->view->form = new Vote_Form_Admin_Category ();
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array () ) );
		// Check post
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			// we will add the category
			$values = $form->getValues ();
			$values ['type'] = $this->_getParam ( 'type', 'photo' );
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				// add category to the database
				// Transaction
				$table = Engine_Api::_ ()->getDbtable ( 'categories', 'vote' );
				$user = Engine_Api::_ ()->user ()->getViewer ();
				
				// insert the category into the database
				$row = $table->createRow ();
				$row->user_id = $user->getIdentity ();
				$row->category_name = $values ["label"];
				$row->type = $values ["type"];
				$row->save ();
				
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ('' ) ) );
		}
	}
	
	public function editCategoryAction() {
		// Generate and assign form
		$form = $this->view->form = new Vote_Form_Admin_Category ();
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array () ) );
		$form->submit->setLabel ( 'Save Changes' );
		
		$category_id = $this->_getParam ( 'id' );
		if (! $category_id) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ('' ) ) );
		}
		
		$table = Engine_Api::_ ()->getDbtable ( 'categories', 'vote' );
		$category = $table->find ( $category_id )->current ();
		
		if (! $category->getIdentity ()) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ('' ) ) );
		}
		$form->label->setValue ( $category->category_name );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			// we will add the category
			$values = $form->getValues ();
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$category->category_name = $values ['label'];
				$category->save ();
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ('' ) ) );
		}
	}
	
	public function deleteCategoryAction() {
		// Generate and assign form
		$form = $this->view->form = new Vote_Form_Delete ();
		
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array () ) );
		$form->submit->setLabel ( 'Delete Category' );
		$form->setTitle ( 'Delete Category' );
		$form->setDescription ( 'Are you sure you want to delete this category?' );
		
		$category_id = $this->_getParam ( 'id' );
		if (! $category_id) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ('' ) ) );
		}
		
		$table = Engine_Api::_ ()->getDbtable ( 'categories', 'vote' );
		$category = $table->find ( $category_id )->current ();
		
		if (! $category->getIdentity ()) {
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ('' ) ) );
		}
		
		if ($this->getRequest ()->isPost ()) {
			// we will add the category
			$values = $form->getValues ();
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$category->delete ();
				$db->commit ();
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'messages' => array ('' ) ) );
		}
	}
	
	public function levelAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'vote_admin_main', array (), 'vote_admin_main_level' );
		
		// Get level id
		if (null !== ($id = $this->_getParam ( 'id' ))) {
			$level = Engine_Api::_ ()->getItem ( 'authorization_level', $id );
		} else {
			$level = Engine_Api::_ ()->getItemTable ( 'authorization_level' )->getDefaultLevel ();
		}
		
		if (! $level instanceof Authorization_Model_Level) {
			throw new Engine_Exception ( 'missing level' );
		}
		
		$level_id = $id = $level->level_id;
		
		// Make form
		$this->view->form = $form = new Vote_Form_Admin_Settings_Level ( array ('public' => (in_array ( $level->type, array ('public' ) )), 'moderator' => (in_array ( $level->type, array ('admin', 'moderator' ) )) ) );
		
		$form->level_id->setValue ( $level_id );
		
		$permissionsTable = Engine_Api::_ ()->getDbtable ( 'permissions', 'authorization' );
		$form->populate ( $permissionsTable->getAllowed ( 'vote', $level_id, array_keys ( $form->getValues () ) ) );
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		// Check validitiy
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		$values = $form->getValues ();
		
		$db = $permissionsTable->getAdapter ();
		$db->beginTransaction ();
		
		try {
			if ($level->type != 'public') {
				// Set permissions
				$values ['auth_comment'] = ( array ) $values ['auth_comment'];
				$values ['auth_view'] = ( array ) $values ['auth_view'];
				$values ['auth_participate'] = ( array ) $values ['auth_participate'];
				$values ['auth_vote'] = ( array ) $values ['auth_vote'];
			}
			$permissionsTable->setAllowed ( 'vote_challenge', $level_id, $values );
			
			// Commit
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
	}

}